﻿using System;
using System.Windows.Forms;
using DbManager.QueryBuilder.DbEntity.Viezd;

namespace TAProject.UI.Order
{
    public partial class FormTourViezd : FormTemplate
    {
        private readonly Form _parentForm;
        private readonly AEntityViezd _aEntityViezd;

        public FormTourViezd(Form parentForm, AEntityViezd aEntityViezd)
            : base(parentForm)
        {
            InitializeComponent();

            _parentForm = parentForm;
            _aEntityViezd = aEntityViezd;

            textBoxAdressClienta.Text = _aEntityViezd.AdressClienta;
            dateTimePickerDataSbora.Value = _aEntityViezd.DataSbora;
            dateTimePickerVremiaSbora.Value = (_aEntityViezd.VremiaSbora > dateTimePickerVremiaSbora.MinDate &&
                                               _aEntityViezd.VremiaSbora < dateTimePickerVremiaSbora.MaxDate)
                                                  ? _aEntityViezd.VremiaSbora
                                                  : dateTimePickerVremiaSbora.MinDate;
            textBoxMestoSbora.Text = !string.IsNullOrEmpty(_aEntityViezd.MestoSbora) ? _aEntityViezd.MestoSbora : _aEntityViezd.AdressClienta;
            numericUpDownPoriadokSbora.Value = _aEntityViezd.PoriadokSbora;

            _parentForm.Enabled = false;
        }

        private void ButtonCancelClick(object sender, EventArgs e)
        {
            FormClose();
        }

        private void FormClose()
        {
            _parentForm.Enabled = true;
            Close();
        }

        private void ButtonSubmitClick(object sender, EventArgs e)
        {
            if (CheckFiilingOfFields())
            {
//                if (dateTimePickerDataSbora.Value < DateTime.Now)
//                {
//                    MessageBox.Show("Дата выезда должна быть больше текущей даты");
//                    return;
//                }
                _aEntityViezd.DataSbora = dateTimePickerDataSbora.Value;
                _aEntityViezd.MestoSbora = textBoxMestoSbora.Text;
                _aEntityViezd.PoriadokSbora = (int) numericUpDownPoriadokSbora.Value;
                _aEntityViezd.VremiaSbora = dateTimePickerVremiaSbora.Value;
                FormClose();
            }
            else
            {
                MessageBox.Show("Не все поля заполнены");
            }
        }

        private bool CheckFiilingOfFields()
        {
            return !string.IsNullOrEmpty(textBoxMestoSbora.Text);
        }
    }
}